﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Edutainment
{
    public class StorageController
    {
        String documentsPath;
        String storageFileName;

        public StorageController()
        {
            storageFileName = "rslt";
            documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        }

        public void saveCurrentState(ArrayList toSave)
        {
            if (toSave != null && toSave.Count > 0)
            {

                using (StreamWriter sWriter = new StreamWriter(File.Open(Path.Combine(documentsPath, storageFileName), FileMode.OpenOrCreate)))
                {
                    StringBuilder sBuilder = new StringBuilder();

                    foreach (SortAlgorithm sortAlgo in toSave)
                    {
                        sBuilder.Clear();
                        String stringToSave = "";
                        String objName = "";

                        if (sortAlgo is BubbleSort)
                            objName = "BubbleSort";
                        else if (sortAlgo is MergeSort)
                            objName = "MergeSort";

                        sBuilder.AppendFormat("{0} {1} {2} {3} {4}\n", objName, sortAlgo.exercises, sortAlgo.solvedExercises, sortAlgo.exams, sortAlgo.solvedExams);
                        stringToSave = sBuilder.ToString();

                        sWriter.Write(stringToSave);
                    }

                }
            }
        }

        public void serializeState()
        {
            AppManager mngr = ((App)App.Current).getAppManager();
            SortationController sController = mngr.sortController;
            ArrayList list = new ArrayList();
            list.Add(sController.bubbleSort);
            list.Add(sController.mergeSort);

            if (list != null && list.Count > 0)
            {
                using (FileStream fs = new FileStream(Path.Combine(documentsPath, storageFileName), FileMode.Create))
                {

                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, list);
                }
            }
        }

        public ArrayList deserializeState()
        {
            String fullPath = Path.Combine(documentsPath, storageFileName);
            if (File.Exists(fullPath))
            {
                using (FileStream fs = new FileStream(fullPath, FileMode.Open))
                {
                    try
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        ArrayList deserialized = bf.Deserialize(fs) as ArrayList;
                        return deserialized;
                    }
                    catch (SerializationException)
                    {
                        return null;
                    }
                    catch (IOException)
                    {
                        return null;
                    }
                }
            }
            return null;
        }

        public ArrayList loadLastState()
        {
            ArrayList list = null;
            String fullPath = Path.Combine(documentsPath, storageFileName);
            if (File.Exists(fullPath))
            {
                using (StreamReader sReader = new StreamReader(File.OpenRead(fullPath)))
                {
                    list = new ArrayList();
                    while (sReader.Peek() >= 0)
                    {
                        String line = sReader.ReadLine();
                        if (line != null)
                        {
                            SortAlgorithm sortRead = null;
                            String[] split = line.Split(' ');
                            if (split.Length == 5)
                            {
                                if (split[0].Equals("BubbleSort"))
                                    sortRead = new BubbleSort(Convert.ToInt32(split[1]), Convert.ToInt32(split[2]), Convert.ToInt32(split[3]), Convert.ToInt32(split[4]));
                                else if (split[0].Equals("MergeSort"))
                                    sortRead = new MergeSort(Convert.ToInt32(split[1]), Convert.ToInt32(split[2]), Convert.ToInt32(split[3]), Convert.ToInt32(split[4]));

                                if (sortRead != null)
                                    list.Add(sortRead);
                            }
                        }
                    }
                }
            }
            return list;
        }
    }
}
